package com.burger.counter.servlets;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.burger.counter.beans.Beverage;
import com.burger.counter.beans.Region;
import com.burger.counter.beans.User;
import com.burger.counter.business.BeverageBusiness;
import com.burger.counter.business.RegionBusiness;
import com.burger.counter.business.UserBusiness;

/**
 * Servlet implementation class EditUserData
 */
public class EditUserData extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EditUserData() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try
		{
			doPost(request, response);
		}
		catch(Exception e)
		{
			System.err.println(e);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try
		{
			// Getting data to user Account Information
			UserBusiness userBI = new UserBusiness();
			User[] userList =null;
			HttpSession session = request.getSession(true);
			
			String emailId = session.getAttribute("UserEmailId").toString();
			
			//Get the details of user from the list
			userList = userBI.getUserDetails(emailId);
			
			userList[0].setEmail(emailId);
			
			//Reverse the DOB Value
			String strDateOfBirth = userList[0].getDateOfBirth().toString();
			String[] strDate = strDateOfBirth.split("-");
			String strYear = strDate[0];
			String strMonth = strDate[1];
			String strDay = strDate[2];
			
			String strDOB =  strMonth + "-" + strDay +"-" + strYear;
			
			session.setAttribute("DOB", strDOB);
			
			session.setAttribute("UserAccountData", userList);
			
			RegionBusiness regionBI = new RegionBusiness();
			Region regionList[] =null;
			try {
				regionList = regionBI.getRegionDetails();
			} 
			catch (Exception e) {
				e.printStackTrace();
			}
				
			Hashtable<Integer,String> regionDetails = new Hashtable<Integer,String>();
			for(int i =0 ;i<regionList.length;i++)
			{
				regionDetails.put(regionList[i].getRegionId(), regionList[i].getRegionName());
			}
			
			request.getSession().setAttribute("regionData", regionDetails);
			
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/EditUserData.jsp");
			dispatcher.forward(request, response);

		}
		catch(Exception e)
		{
			System.err.println(e);
		}
	}

}
